<?php
// No directAcces
defined('_AppBackMiage') or die('Restricted access');
if(isAdministrateur ()){
    include_once (DS . "backoffice/include/header.php");

    if (isset($_GET['uid']) && !empty($_GET['uid']))
    {
        $contact = new ContactEntreprise($_GET['uid']);

        if (isset($_POST['submit']))
        {
            // variable qui va nous servir pour savoir s'il y a a eu une erreur ;)
            $form = true;
            $errMSG = array();
            if (isset($_POST['lastname']) && !empty($_POST['lastname']))
            {
                $tmp = $contact->setNomUtilisateur($_POST['lastname']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['nom'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            if (isset($_POST['firstname']) && !empty($_POST['firstname']))
            {
                $tmp = $contact->setPrenomUtilisateur($_POST['firstname']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['prenom'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            if (isset($_POST['email']) && !empty($_POST['email']))
            {
                $tmp = $contact->setEmailUtilisateur($_POST['email']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['mail'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if (isset($_POST['adresse']) && !empty($_POST['adresse']))
            {
                $tmp = $contact->setAdresse1($_POST['adresse']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['add1'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            if (isset($_POST['adresse2']))
            {
                $tmp = $contact->setAdresse2($_POST['adresse2']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['add2'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            if (isset($_POST['city']) && !empty($_POST['city']))
            {
                $tmp = $contact->setVille($_POST['city']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['ville'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            if (isset($_POST['cp']) && !empty($_POST['cp']))
            {
                $tmp = $contact->setCP($_POST['cp']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['cp'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if (isset($_POST['entreprise']) && !empty($_POST['entreprise']))
            {
                $tmp = $contact->setIdEntreprise($_POST['entreprise']);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['entreprise'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if (isset($_POST['types']) && !empty($_POST['types']))
            {
                $types = implode(',', $_POST['types']);
                $tmp = $contact->setIdTypeUser($types);
                if ($tmp['boolOk'] === false)
                {
                    $errMSG['types'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            else
            {
                $contact->setIdTypeUser("");
            }

            if ($form === true)
            {
                $contact->update();
            }
        }
    }
    else
    {
        redirectTo(URLBACK."_usercontact_list");
    }

    $lstEntreprise = Entreprise::getList();
    $lstTypes = TypeUtilisateur::getList();
    ?>
    <?php
    include_once( DS . "backoffice/include/toppanel_utilisateurs.php");
    ?>
    <div id="wrapper">
        <div id="content">
            <div id="box">
                <h3 id="adduser">Edition d'un contact entreprise</h3>
                <form id="form" method="post">
                    <fieldset id="personal">
                        <legend>Informations Personnelles</legend>

                        <label for="lastname">Nom : </label>
                        <input name="lastname" id="lastname" type="text" tabindex="1" value="<?php echo $contact->getNomUtilisateur(); ?>" />
                        <span class="error"><?php
    if (isset($errMSG['nom']))
    {
        echo $errMSG['nom'];
    }
    ?></span>
                        <br />
                        <label for="firstname">Prenom : </label>
                        <input name="firstname" id="firstname" type="text" tabindex="2" value="<?php echo $contact->getPrenomUtilisateur(); ?>"/>
                        <span class="error"><?php
                            if (isset($errMSG['prenom']))
                            {
                                echo $errMSG['prenom'];
                            }
    ?></span>
                        <br />
                        <label for="email">Email : </label>
                        <input name="email" id="email" type="text"tabindex="2" value="<?php echo $contact->getEmailUtilisateur(); ?>" />
                        <span class="error"><?php
                            if (isset($errMSG['mail']))
                            {
                                echo $errMSG['mail'];
                            }
    ?></span>
                        <br />
                        <p>Envoyer Mot de passe <input name="generatepass" id="yes" type="checkbox"  value="yes" tabindex="35" /></p>
                    </fieldset>
                    <fieldset id="address">
                        <legend>Adresse</legend>
                        <label for="adresse">Adresse : </label>
                        <input name="adresse" id="adresse" type="text" tabindex="1" value="<?php echo $contact->getAdresse1(); ?>" />
                        <span class="error"><?php
                            if (isset($errMSG['add1']))
                            {
                                echo $errMSG['add1'];
                            } ?></span>
                        <br />
                        <label for="adresse2">Adresse + : </label>
                        <input name="adresse2" id="adresse2" type="text" tabindex="1" value="<?php echo $contact->getAdresse2(); ?>" />
                        <span class="error"><?php
                            if (isset($errMSG['add2']))
                            {
                                echo $errMSG['add2'];
                            }
    ?></span>
                        <br />
                        <label for="city">Ville : </label>
                        <input name="city" id="city" type="text" tabindex="2" value="<?php echo $contact->getVille(); ?>"/>
                        <span class="error"><?php
                            if (isset($errMSG['ville']))
                            {
                                echo $errMSG['ville'];
                            }
    ?></span>
                        <br />
                        <label for="cp">Code Postal : </label>
                        <input name="cp" id="cp" type="text" tabindex="1" value="<?php echo $contact->getCP(); ?>"/>
                        <span class="error"><?php
                            if (isset($errMSG['cp']))
                            {
                                echo $errMSG['cp'];
                            } ?></span>
                        <br />
                    </fieldset>
                    <fieldset id="opt">
                        <legend>OPTIONS</legend>
                        <label for="entreprise">Entreprise : </label>
                        <select name="entreprise">
                            <?php
                            if (!empty($lstEntreprise))
                            {
                                foreach ($lstEntreprise as $p)
                                {
                            ?>
                                    <option value="<?php echo $p->IdEntreprise; ?>" <?php
                                    if ($p->IdEntreprise == $contact->getIdEntreprise())
                                    {
                                        echo "selected='selected'";
                                    } ?>>
                        <?php echo $p->NomEntreprise; ?>
                                        </option>
                        <?php
                                }
                            }
                        ?>
                            </select>
                            <br/>
                            <label for="types">Type d'utilisateur : </label>
                        <?php
                            if (!empty($lstTypes))
                            {
                                $typesUser = explode(',', $contact->getIdTypeUser());
                                foreach ($lstTypes as $type)
                                {
                        ?>
                            <?php echo $type->NomTypeUtilisateur; ?>
                                <input type="checkbox" name="types[]" value="<?php echo $type->IdTypeUtilisateur; ?>" <?php
                                    if (in_array($type->IdTypeUtilisateur, $typesUser))
                                    {
                                        echo "checked='checked'";
                                    }
                            ?>>
                                &nbsp;|&nbsp;
    <?php
                                }
                            }
    ?>
                                            <br /><span class="error"><?php
                            if (isset($errMSG['types']))
                            {
                                echo $errMSG['types'];
                            }
    ?></span>
                </fieldset>
                <div align="center">
                    <input id="button1" type="submit" name="submit" value="Sauver" />
                    <input id="button2" type="reset" />
                </div>
            </form>
        </div>
    </div>
    <?php
    include_once (DS . "backoffice/include/sidebar.php");
    include_once (DS . "backoffice/include/footer.php");
}
else
{
    redirectTo(URLBACK);
}
?>